package com.zyw.customcalendar.operation;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.TimeZone;
import com.zyw.customcalendar.AppApplication;
import com.zyw.customcalendar.dao.CalendarSQLiteDao;

public class CalendarOperation implements CalendarInterface{

	//monthDays[i]=0��������ֵΪ��
	public int[] monthDays = {
			0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0
	};
	
	public HashMap<String,Integer> eventDays;
	
	protected String[] nlDays = new String[monthDays.length];
	//
	protected String[] weeks = {
			"一","二","三","四","五","六","日"
	};
	
	private CalendarSQLiteDao sqliteDao;
	
	//fstMonthDayWeek:ÿ����һ�������ڼ�
	//private int fstMonthDayWeek = -1;
	//selectDate:ѡ�������
	private Calendar selectCalendar;
	//currentDate:��ǰ����
	private final Calendar currentCalendar = Calendar.getInstance();
	//�����ǵڼ���
	public int currentDayOfMonth = -1;
	public int selectDayOfMonth = -1;

	public CalendarOperation(){
		currentCalendar.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
		selectCalendar = currentCalendar;
		eventDays = new HashMap<>();
		sqliteDao = new CalendarSQLiteDao();
		getMonthDays();
	}

	protected void getMonthDays(){
		//ȫ������Ϊ0
		for(int i=0 ;i<monthDays.length; i++){
			monthDays[i] = 0;
			nlDays[i] = null;
		}
		
		int weeks = selectCalendar.get(Calendar.DAY_OF_WEEK);
		int days = selectCalendar.get(Calendar.DAY_OF_MONTH);

		int fstMonthDayWeek = -1;
		//���㵱��1�������ڼ�
		fstMonthDayWeek = (weeks-1) == 0 ? 7:(weeks-1);
		for(int i = days; i > 1; i--){
			fstMonthDayWeek--;
			if(fstMonthDayWeek == 0){
				fstMonthDayWeek = 7;
			}
		}

		//�������������ڶ�Ӧ��ϵmonthDays
		int year = selectCalendar.get(Calendar.YEAR);
		int month = selectCalendar.get(Calendar.MONTH)+1;

		int currentMonth = currentCalendar.get(Calendar.MONTH) + 1;
		android.util.Log.i("zyw","month="+month+" /currentMonth="+currentMonth);
		
		//����¼�ǰ�����ԭ���¼�
		int currentMonthDayWeek = 1;
		int allMonthDays = selectCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		for(int i = fstMonthDayWeek-1; i < allMonthDays + fstMonthDayWeek-1; i++){
			monthDays[i] = currentMonthDayWeek;
			long[] l = ChinaDate.calElement(year, month, currentMonthDayWeek);
			nlDays[i] = ChinaDate.getChinaDate((int) (l[2]));
			currentMonthDayWeek++;
		}

		if(null != AppApplication.sqlite)
			addEvent(month, year);
		
		currentDayOfMonth = selectCalendar.get(Calendar.DAY_OF_MONTH);
		android.util.Log.i("zyw","currentDayOfMonth = " + currentDayOfMonth);
		if(selectDayOfMonth == -1){
			if(month == currentMonth)
				selectDayOfMonth = currentDayOfMonth;
			else
				selectDayOfMonth = 1;
		}
	}

	private void addEvent(int month, int year){
		String dayts = month + "";
		if(month < 10)
			dayts = "0" + month;
		String monthString = year + "/" + dayts;

		eventDays.clear();
		sqliteDao.getEventsLikeMonth(monthString, eventDays);
	}
	
	protected void setSelectDate(Calendar selectCalendar){
		this.selectCalendar = selectCalendar;
	}
	
	public String returnCurrentMonths(){
		int year = selectCalendar.get(Calendar.YEAR);
		int months = selectCalendar.get(Calendar.MONTH) + 1;
		return year + "年" + months + "月";
	}
	
	public String returnCurrentDays(){
		int year = selectCalendar.get(Calendar.YEAR);
		int months = selectCalendar.get(Calendar.MONTH) + 1;
		
		String days = months + "";
		if(months < 10)
			days = "0" + months;
		
		if(currentDayOfMonth < 10)
			days += "/0" + currentDayOfMonth;
		else
			days += "/" + currentDayOfMonth;
		
		return year + "/" + days;
	}

	public String returnDays(int day){
		int year = selectCalendar.get(Calendar.YEAR);
		int months = selectCalendar.get(Calendar.MONTH) + 1;

		String days = months + "";
		if(months < 10)
			days = "0" + months;

		if(day < 10)
			days += "/0" + day;
		else
			days += "/" + day;

		return year + "/" + days;
	}
	
	public String returnSelectDays(){
		int year = selectCalendar.get(Calendar.YEAR);
		int months = selectCalendar.get(Calendar.MONTH) + 1;
		
		String days = months + "";
		if(months < 10)
			days = "0" + months;

		if(selectDayOfMonth == -1){
			selectDayOfMonth = currentDayOfMonth;
		}

		if(selectDayOfMonth < 10)
			days += "/0" + selectDayOfMonth;
		else
			days += "/" + selectDayOfMonth;
		
		return year + "/" + days;
	}
	
	public String returnCurrentTimes(){
		@SuppressWarnings("static-access")
		int hour = selectCalendar.HOUR_OF_DAY;
		@SuppressWarnings("static-access")
		int minute = selectCalendar.MINUTE;
		
		String times = hour + "";
		if(hour < 10)
			times = "0" + hour;
		
		if(minute < 10)
			times += ":0" + minute;
		else
			times += ":" + minute;
		return times;
	}

	
	//���⿪�ŵĽӿ�
	@Override
	public Calendar returnSelectCalendar() {
		// TODO Auto-generated method stub
		return selectCalendar;
	}

	@Override
	public Calendar returnCurrentCalendar() {
		// TODO Auto-generated method stub
		return currentCalendar;
	}

	@Override
	public boolean setSelectCalendar(Calendar selectCalendar) {
		// TODO Auto-generated method stub
		if(selectCalendar != null){
			this.selectCalendar = selectCalendar;
			return true;
		}
		return false;
	}

	@Override
	public int returnCDayOfMonth() {
		// TODO Auto-generated method stub
		return currentDayOfMonth;
	}

	@Override
	public int returnSDayOfMonth() {
		// TODO Auto-generated method stub
		return selectCalendar.get(Calendar.DAY_OF_MONTH);
	}

	@Override
	public int returnCWeekOfMonth() {
		// TODO Auto-generated method stub
		return currentCalendar.get(Calendar.WEEK_OF_MONTH);
	}

	@Override
	public int returnSWeekOfMonth() {
		// TODO Auto-generated method stub
		return selectCalendar.get(Calendar.WEEK_OF_MONTH);
	}

	@Override
	public void setNextCalendar(Calendar calendar) {
		// TODO Auto-generated method stub
		calendar.add(Calendar.MONTH, 1);//�õ���һ����
		selectCalendar = calendar;
		getMonthDays();
	}

	@Override
	public void setPreviousCalendar(Calendar calendar) {
		// TODO Auto-generated method stub
		calendar.add(Calendar.MONTH, -1);//�õ���һ����
		selectCalendar = calendar;
		getMonthDays();
	}

	@Override
	public void refreshCalendar() {
		// TODO Auto-generated method stub
		getMonthDays();
	}
}
